package com.emoji.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emoji.model.Emoji;
import com.emoji.service.IEmojiService;
import com.google.gson.Gson;

@Controller
public class EmojiController extends BaseController{

  	public static final String AJAX_SUCC = "OK";
  	public static final String AJAX_FAIL = "FAIL";
  	
    @Autowired
    IEmojiService emojiService;
  	
    /**
     * PC端 浏览器访问 
     * 添加emoji表情页面
     * @param model
     * @param id
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/add")
    public String add(Model model, @RequestParam(value = "id", required = false) Integer id){
    	try {
    		if(id != null && id.intValue() > 0){
	    		Emoji emoji = emojiService.selectByPrimaryKey(id);
	    		model.addAttribute("title", emoji.getTitle());
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return "add";
    }
    
    /**
     * PC端 浏览器访问
     * 保存emoji表情方法
     * @param model
     * @param id
     * @param title
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/add_save")
    public String addSave(Model model, 
    		@RequestParam(value = "id", required = false) Integer id,
    		@RequestParam(value = "title", required = false) String title) {
        
        try {
        	if(id != null && id.intValue() > 0 && StringUtils.isNotBlank(title)){
        		Emoji emoji = emojiService.selectByPrimaryKey(id);
        		emoji.setTitle(title);
	            if (emojiService.update(emoji) > 0) {
	            	model.addAttribute("title", emoji.getTitle());
	            	return "redirect:add.do?id="+emoji.getId();
	            }
        	}else if((id == null || id.intValue() <= 0) && StringUtils.isNotBlank(title)){
        		Emoji emoji = new Emoji();
        		emoji.setTitle(title);
        		emoji.setUtf8Title(title);
        		if(emojiService.save(emoji) > 0){
        			return "redirect:add.do?id="+emoji.getId();
        		}
        	}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:add.do";
    }
    
    /**
     * PC端 浏览器访问
     * 查看emoji表情页面
     * @param model
     * @param id
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/view")
    public String view(Model model){
    	try {
    		Emoji emoji = emojiService.getByOne();
    		model.addAttribute("title", emoji.getTitle());
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return "view";
    }
    
    /**
     * 移动端 浏览器访问
     * 添加emoji表情H5页面
     * @param model
     * @param id
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/h5_add")
    public String h5Add(Model model, @RequestParam(value = "id", required = false) Integer id){
    	try {
    		if(id != null && id.intValue() > 0){
	    		Emoji emoji = emojiService.selectByPrimaryKey(id);
	    		model.addAttribute("title", emoji.getTitle());
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return "h5_add";
    }
  	
    /**
     * 移动端 浏览器访问
     * 保存emoji表情方法
     * @param model
     * @param id
     * @param title
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/h5_add_save")
    public String h5AddSave(Model model, 
    		@RequestParam(value = "id", required = false) Integer id,
    		@RequestParam(value = "title", required = false) String title) {
        
        try {
        	if(id != null && id.intValue() > 0 && StringUtils.isNotBlank(title)){
        		Emoji emoji = emojiService.selectByPrimaryKey(id);
        		emoji.setTitle(title);
	            if (emojiService.update(emoji) > 0) {
	            	model.addAttribute("title", emoji.getTitle());
	            	return "redirect:h5_add.do?id="+emoji.getId();
	            }
        	}else if((id == null || id.intValue() <= 0) && StringUtils.isNotBlank(title)){
        		Emoji emoji = new Emoji();
        		emoji.setTitle(title);
        		emoji.setUtf8Title(title);
        		if(emojiService.save(emoji) > 0){
        			return "redirect:h5_add.do?id="+emoji.getId();
        		}
        	}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:h5_add.do";
    }
    
    /**
     * 移动端 APP访问
     * 保存emoji表情方法
     * @param id
     * @param title
     * @return
     * @author Josh
     */
    @RequestMapping(value = "/save")
    public @ResponseBody String save(
    		@RequestParam(value = "id", required = false) Integer id,
    		@RequestParam(value = "title", required = false) String title) {
        
        try {
        	if(id != null && id.intValue() > 0){
            	Emoji emoji = emojiService.selectByPrimaryKey(id);
            	emoji.setTitle(title);
            	emoji.setUtf8Title(title);
                if (emojiService.update(emoji) > 0) {
                    return AJAX_SUCC;
                }
        	}else{
            	Emoji emoji = new Emoji();
            	emoji.setTitle(title);
            	emoji.setUtf8Title(title);
                if (emojiService.save(emoji) > 0) {
                    return AJAX_SUCC;
                }
        	}

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return AJAX_FAIL;
    }
    
    /**
     * 移动端 APP访问
     * 根据id获得emoji表情方法
     * @param response
     * @param id
     * @author Josh
     */
    @RequestMapping(value = "/get")
    public void get(HttpServletResponse response) {
    	
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        
    	Map<String, Object> retMap = new HashMap<String, Object>();
        try {
        	
        	
        	Emoji emoji = emojiService.getByOne();
        	
        	retMap.put("title", emoji==null||StringUtils.isEmpty(emoji.getTitle())?"暂无数据！":emoji.getTitle());
        	
            Gson gson = new Gson();
            String json = gson.toJson(retMap);

            PrintWriter writer = null;
            try {
                writer = response.getWriter();
                writer.print(json);
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}